package com.approval.approvalapp.index.dbsx.zxhf;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.TshfDbXqBean;
import com.approval.approvalapp.index.dbsx.tshf.BaseActivity2;
import com.approval.approvalapp.index.dbsx.tshf.TshfContantActivity;
import com.approval.approvalapp.index.dbsx.zxhf.adapter.ZdjgAdapter;
import com.approval.approvalapp.index.dbsx.zxhf.fragment.ZxHfDbFragment;
import com.approval.approvalapp.index.dbsx.zxhf.fragment.ZxHfYbFragment;
import com.approval.approvalapp.util.DensityUtil;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//回复咨询
public class ZxhfContantActivity extends BaseActivity2 {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.send_tv)
    TextView sendTv;
    @Bind(R.id.zdjg_tv)
    TextView zdjgTv;
    @Bind(R.id.et)
    EditText et;
    @Bind(R.id.zdjg_rl)
    RelativeLayout zdjg_rl;
    private List<TshfDbXqBean.ApplydetailBean.PassuserBean> list;

    @Override
    public int getLayoutId() {
        return R.layout.activity_zxhf_contant;
    }

    @Override
    public void initView() {
        list = (List<TshfDbXqBean.ApplydetailBean.PassuserBean>) getIntent().getSerializableExtra("list");
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.back_iv, R.id.send_tv, R.id.zdjg_rl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.zdjg_rl:
                new XPopup.Builder(this)
                        .autoOpenSoftInput(true)
                        .asCustom(new CustomPopup(this))
                        .show();
                break;
            case R.id.send_tv:
                if (et.getText().toString().trim().length() == 0) {
                    Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                List<String> uid = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).isSelect()) {
                        uid.add(list.get(i).getPassuserid());
                    }
                }
                if (uid.size() == 0) {
                    Toast.makeText(this, "请选择指定机构", Toast.LENGTH_SHORT).show();
                    return;
                }
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < uid.size(); i++) {
                    if (i == uid.size() - 1) {
                        sb.append("" + uid.get(i));
                    } else {
                        sb.append(uid.get(i) + ",");

                    }
                }

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("username", SpUtil.getString(this, SpUtil.USER_NAME, ""));
                jsonObject.put("applyid", getIntent().getStringExtra("applyid"));
                jsonObject.put("passuserid", sb.toString());
                jsonObject.put("opinion", et.getText().toString().trim());
                jsonObject.put("passnode", getIntent().getIntExtra("passnode",0));
                jsonObject.put("currentNode", getIntent().getIntExtra("currentNode",0));
                jsonObject.put("handleId", getIntent().getStringExtra("handleId"));
                NetRequsetUtil.getInstance().netRequestPostJson("v1/user/handle_zx_hf", jsonObject.toString(), new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        if (ZxHfDbFragment.instance != null) {
                            ZxHfDbFragment.instance.refreshData();
                        }
                        if (ZxHfYbFragment.instance != null) {
                            ZxHfYbFragment.instance.refreshData();
                        }
                        ZxhfContantActivity.this.finish();
                        if (ZxhfxqDbActivity.instance != null) {
                            ZxhfxqDbActivity.instance.finish();
                        }
                    }

                    @Override
                    public void errowithresponse(String str) {
                        ErrorBeanUtil.getErrorMessage(str);
                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {

                    }
                });
                break;
        }
    }

    public class CustomPopup extends CenterPopupView {
        public CustomPopup(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_popup_zdjg;
        }

        @Override
        protected void onCreate() {
            super.onCreate();

            ImageView iv = findViewById(R.id.close);
            iv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    List<String> uid = new ArrayList<>();
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).isSelect()) {
                            uid.add(list.get(i).getPassuserid());
                        }
                    }
                    if (uid == null || uid.size() == 0) {
                        zdjgTv.setText("请选择");
                    } else {
                        zdjgTv.setText("已选择" + uid.size());
                    }
                }
            });
            TextView commit_tv = findViewById(R.id.commit_tv);
            commit_tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    List<String> uid = new ArrayList<>();
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).isSelect()) {
                            uid.add(list.get(i).getPassuserid());
                        }
                    }
                    if (uid == null || uid.size() == 0) {
                        zdjgTv.setText("请选择");
                    } else {
                        zdjgTv.setText("已选择" + uid.size());
                    }
                }
            });
            RecyclerView recyclerView = findViewById(R.id.rv);
            recyclerView.setLayoutManager(new LinearLayoutManager(ZxhfContantActivity.this));
            ZdjgAdapter zdjgAdapter = new ZdjgAdapter();
            recyclerView.setAdapter(zdjgAdapter);
            zdjgAdapter.setList(list);


        }

        @Override
        protected void onShow() {
            super.onShow();
        }

//        @Override
//        protected int getMaxWidth() {
//            return DensityUtil.dip2px(ZxhfContantActivity.this, 330f);
//        }

    }
}
